
###Clear R--------------------
rm(list=ls())

zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}
###Load data-----------
load("Study1_data.RData")

###Function to recode variables to range from lowest (0) to highest (1) observation---------------------
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}


####function to install packages if they don't exist----------------
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}
# usage
packages <- c("ggplot2", "psych","interplot","stargazer","dplyr","stringr","tidyr", "xtable")
ipak(packages)

### Descriptive statistics in the paper----------------

#Partisan Social Identity Strength
summary(data$partyidentity)
sd(data$partyidentity, na.rm=T)
skewness(data$partyidentity, na.rm=T)
kurtosis(data$partyidentity, na.rm=T)
psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(huddy1, huddy2, huddy3, huddy4, huddy5, huddy6, huddy7, huddy8))),check.keys = T)

#CRT
summary(data$CRTall)
sd(data$CRTall)
skewness(data$CRTall, na.rm=T)
kurtosis(data$CRTall, na.rm=T)
psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(CRT1, CRT2, CRT3))),check.keys = T)

#Need for Cognition
summary(data$nfc)
sd(data$nfc)
skewness(data$nfc, na.rm=T)
kurtosis(data$nfc, na.rm=T)
psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(NfC_1,NfC_2,NfC_3,NfC_4,NfC_5,NfC_6,NfC_7,NfC_8,NfC_9,NfC_10,NfC_11,NfC_12,NfC_13,NfC_14,NfC_15,NfC_16,NfC_17,NfC_18))),check.keys = T)

#Cognitive resources
data$cogresources<-zero1(data$cogresources)
summary(data$cogresources)
sd(data$cogresources)
skewness(data$cogresources)
kurtosis(data$cogresources)

alpha_cog<-data.frame(data$CRT1, data$CRT2, data$CRT3, data$NfC_1, data$NfC_2, data$NfC_3_rec, data$NfC_4_rec, data$NfC_5_rec , data$NfC_6, data$NfC_7_rec, data$NfC_8_rec, data$NfC_9_rec, data$NfC_10, data$NfC_11, data$NfC_12_rec, data$NfC_13, data$NfC_14, data$NfC_15, data$NfC_16_rec, data$NfC_17_rec, data$NfC_18)
psych::alpha(alpha_cog)

#Correlation between moderators
cor.test(data$CRTall, data$nfc)
cor.test(data$CRTall, data$cogresources)
cor.test(data$nfc, data$cogresources)
cor.test(data$partyidentity, data$CRTall)
cor.test(data$partyidentity, data$nfc)
cor.test(data$partyidentity, data$cogresources)

### Main effects of treatment on policy support-----------------------------
summary(lm(DV_irradiation~InParty+OutParty+partyidentity+CRTall+age+female+non_white+as.factor(education)+Republican_dummy,data))
summary(lm(DV_irradiation~InParty+OutParty+partyidentity+nfc+age+female+non_white+as.factor(education)+Republican_dummy,data))
summary(lm(DV_irradiation~InParty+OutParty+partyidentity+cogresources+age+female+non_white+as.factor(education)+Republican_dummy,data))

###Table 2: Food Irradiation: Policy support, party cues, cognitive resources and social identity strength--------------
twoway_crt<-(lm(DV_irradiation~InParty+OutParty+partyidentity+CRTall+InParty*partyidentity+OutParty*partyidentity+InParty*CRTall+OutParty*CRTall+partyidentity*CRTall+age+female+non_white+as.factor(education)+Republican_dummy,data))
twoway_nfc<-(lm(DV_irradiation~InParty+OutParty+partyidentity+nfc+InParty*partyidentity+OutParty*partyidentity+InParty*nfc+OutParty*nfc+partyidentity*nfc+age+female+non_white+as.factor(education)+Republican_dummy,data))
twoway_cog<-(lm(DV_irradiation~InParty+OutParty+partyidentity+cogresources+InParty*partyidentity+OutParty*partyidentity+InParty*cogresources+OutParty*cogresources+partyidentity*cogresources+age+female+non_white+as.factor(education)+Republican_dummy,data))

#replace names 2 way model
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "nfc"] <- "CRTall"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "InParty:nfc"] <- "InParty:CRTall"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "OutParty:nfc"] <- "OutParty:CRTall"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "partyidentity:nfc"] <- "partyidentity:CRTall"

names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "cogresources"] <- "CRTall"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "InParty:cogresources"] <- "InParty:CRTall"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "OutParty:cogresources"] <- "OutParty:CRTall"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "partyidentity:cogresources"] <- "partyidentity:CRTall"

threeway_crt<-(lm(DV_irradiation~InParty+OutParty+partyidentity+CRTall+InParty*partyidentity+OutParty*partyidentity+InParty*CRTall+OutParty*CRTall+partyidentity*CRTall+InParty*partyidentity*CRTall+OutParty*partyidentity*CRTall +age+female+non_white+as.factor(education)+Republican_dummy,data))
threeway_nfc<-(lm(DV_irradiation~InParty+OutParty+partyidentity+nfc+InParty*partyidentity+OutParty*partyidentity+InParty*nfc+OutParty*nfc+partyidentity*nfc+InParty*partyidentity*nfc+OutParty*partyidentity*nfc+age+female+non_white+as.factor(education)+Republican_dummy,data))
threeway_cog<-(lm(DV_irradiation~InParty+OutParty+partyidentity+cogresources+InParty*partyidentity+OutParty*partyidentity+InParty*cogresources+OutParty*cogresources+partyidentity*cogresources+InParty*partyidentity*cogresources+OutParty*partyidentity*cogresources+age+female+non_white+as.factor(education)+Republican_dummy,data))

#replace names 3 way model
names(threeway_nfc$coefficients)[names(threeway_nfc$coefficients) == "nfc"] <- "CRTall"
names(threeway_nfc$coefficients)[names(threeway_nfc$coefficients) == "InParty:nfc"] <- "InParty:CRTall"
names(threeway_nfc$coefficients)[names(threeway_nfc$coefficients) == "OutParty:nfc"] <- "OutParty:CRTall"
names(threeway_nfc$coefficients)[names(threeway_nfc$coefficients) == "partyidentity:nfc"] <- "partyidentity:CRTall"
names(threeway_nfc$coefficients)[names(threeway_nfc$coefficients) == "InParty:partyidentity:nfc"] <- "InParty:partyidentity:CRTall"
names(threeway_nfc$coefficients)[names(threeway_nfc$coefficients) == "OutParty:partyidentity:nfc"] <- "OutParty:partyidentity:CRTall"

names(threeway_cog$coefficients)[names(threeway_cog$coefficients) == "cogresources"] <- "CRTall"
names(threeway_cog$coefficients)[names(threeway_cog$coefficients) == "InParty:cogresources"] <- "InParty:CRTall"
names(threeway_cog$coefficients)[names(threeway_cog$coefficients) == "OutParty:cogresources"] <- "OutParty:CRTall"
names(threeway_cog$coefficients)[names(threeway_cog$coefficients) == "partyidentity:cogresources"] <- "partyidentity:CRTall"
names(threeway_cog$coefficients)[names(threeway_cog$coefficients) == "InParty:partyidentity:cogresources"] <- "InParty:partyidentity:CRTall"
names(threeway_cog$coefficients)[names(threeway_cog$coefficients) == "OutParty:partyidentity:cogresources"] <- "OutParty:partyidentity:CRTall"


stargazer(twoway_crt, threeway_crt,twoway_nfc, threeway_nfc, twoway_cog, threeway_cog, title="Food Irradiation: Policy support, party cues, cognitive resources and social identity strength", align=TRUE,  notes.append = FALSE, order=c(1,2,3,4, 11, 12, 13,14,15,16,17,5,6,7,8,9,10), covariate.labels=c("In-party cue", "Out-party cue", "Partisan Identity Strength (PSID)","Cognitive resource", "In-party * PSID", "Out-party * PSID", "In-party * Cognitive", "Out-party * Cognitive", "PSID * Cognitive", "In-party * PSID * Cognitive", "Out-party * PSID * Cognitive", "Age", "Female", "Race: non-white", "Education: Some college", "Education: College", "Party: Republican", "Constant"),
          star.cutoffs=c(0.05), omit.stat=c("LL","ser","f", "adj.rsq"), 
          notes = "*p<0.05", 
          column.sep.width = "1pt",
          no.space=TRUE, font.size="tiny" ,  out = "Study1_summary.tex",dep.var.caption = "Policy support",column.labels = c("CRT", "NfC", "Cog resources"), column.separate = c(2,2,2), dep.var.labels.include = FALSE,label="tab:stud2", digits=2)

### Figure 1:  FFood Irradiation: Marginal Effect of In-party and Out-party Cues on Policy Support across Levels of Partisan Social Identity and Cognitive Resources-------------------------------------

## CRT at 0 or -1SD
m0 <- lm(DV_irradiation~InParty*scale(CRTall,center=mean(CRTall)-sd(CRTall),scale=F)*partyidentity+OutParty*scale(CRTall,center=mean(CRTall)-sd(CRTall),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
summary(m0)
m_0_out <- interplot(m0,var1 = "OutParty",var2='partyidentity')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='partyidentity')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'
## CRT at mean
m1 <- lm(DV_irradiation~InParty*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*partyidentity+OutParty*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
summary(m1)
m_1_out <- interplot(m1,var1 = "OutParty",var2='partyidentity')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='partyidentity')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'
## CRT at +1
m2 <- lm(DV_irradiation~InParty*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*partyidentity+ OutParty*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='partyidentity')
summary(m2)
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='partyidentity')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot_crt <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot_crt$battery      <- c("CRT")
forplot_crt$CRT <- factor(forplot_crt$CRT,levels = c("-1 SD","Mean","+1 SD"))

##########NFC
## NFC -1SD
m0_nfc <- lm(DV_irradiation~InParty*scale(nfc,center=mean(nfc)-sd(nfc),scale=F)*partyidentity+ OutParty*scale(nfc,center=mean(nfc)-sd(nfc),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
summary(m0_nfc)
m_0_in_nfc <- interplot(m0_nfc,var1 = "InParty",var2='partyidentity')
m_0_in_nfc$data$CRT='-1 SD'
m_0_in_nfc$data$Cue='In'
m_0_out_nfc <- interplot(m0_nfc,var1 = "OutParty",var2='partyidentity')
m_0_out_nfc$data$CRT='-1 SD'
m_0_out_nfc$data$Cue='Out'

## NfC at mean
m1_nfc <- lm(DV_irradiation~InParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity+OutParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
summary(m1_nfc)
m_1_in_nfc <- interplot(m1_nfc,var1 = "InParty",var2='partyidentity')
m_1_in_nfc$data$CRT='Mean'
m_1_in_nfc$data$Cue='In'
m_1_out_nfc <- interplot(m1_nfc,var1 = "OutParty",var2='partyidentity')
m_1_out_nfc$data$CRT='Mean'
m_1_out_nfc$data$Cue='Out'

## NfC at +1
m2_nfc <- lm(DV_irradiation~InParty*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity+ OutParty*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
summary(m2_nfc)
m_2_in_nfc <- interplot(m2_nfc,var1 = "InParty",var2='partyidentity')
m_2_in_nfc$data$CRT='+1 SD'
m_2_in_nfc$data$Cue='In'
m_2_out_nfc <- interplot(m2_nfc,var1 = "OutParty",var2='partyidentity')
m_2_out_nfc$data$CRT='+1 SD'
m_2_out_nfc$data$Cue='Out'

forplot_nfc <- rbind(m_0_in_nfc$data, m_0_out_nfc$data,m_1_in_nfc$data,m_1_out_nfc$data,m_2_in_nfc$data,m_2_out_nfc$data)
forplot_nfc$battery      <- c("NfC")
forplot_nfc$CRT <- factor(forplot_nfc$CRT,levels = c("-1 SD","Mean","+1 SD"))

##########Cog resoucres
## Cog resoucrres -1SD
m0_nfc <- lm(DV_irradiation~InParty*scale(cogresources,center=mean(cogresources)-sd(cogresources),scale=F)*partyidentity+ OutParty*scale(cogresources,center=mean(cogresources)-sd(cogresources),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_0_in_nfc <- interplot(m0_nfc,var1 = "InParty",var2='partyidentity')
m_0_in_nfc$data$CRT='-1 SD'
m_0_in_nfc$data$Cue='In'
m_0_out_nfc <- interplot(m0_nfc,var1 = "OutParty",var2='partyidentity')
m_0_out_nfc$data$CRT='-1 SD'
m_0_out_nfc$data$Cue='Out'

##  Cog resoucrres  at mean
m1_nfc <- lm(DV_irradiation~InParty*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*partyidentity+OutParty*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_1_in_nfc <- interplot(m1_nfc,var1 = "InParty",var2='partyidentity')
m_1_in_nfc$data$CRT='Mean'
m_1_in_nfc$data$Cue='In'
m_1_out_nfc <- interplot(m1_nfc,var1 = "OutParty",var2='partyidentity')
m_1_out_nfc$data$CRT='Mean'
m_1_out_nfc$data$Cue='Out'

##  Cog resoucrres  at +1
m2_nfc <- lm(DV_irradiation~InParty*scale(cogresources,center=mean(cogresources)+sd(cogresources),scale=F)*partyidentity+ OutParty*scale(cogresources,center=mean(cogresources)+sd(cogresources),scale=F)*partyidentity+age+female+non_white+as.factor(education)+Republican_dummy,data)
m_2_in_nfc <- interplot(m2_nfc,var1 = "InParty",var2='partyidentity')
m_2_in_nfc$data$CRT='+1 SD'
m_2_in_nfc$data$Cue='In'
m_2_out_nfc <- interplot(m2_nfc,var1 = "OutParty",var2='partyidentity')
m_2_out_nfc$data$CRT='+1 SD'
m_2_out_nfc$data$Cue='Out'

forplot_cog <- rbind(m_0_in_nfc$data, m_0_out_nfc$data,m_1_in_nfc$data,m_1_out_nfc$data,m_2_in_nfc$data,m_2_out_nfc$data)
forplot_cog$battery      <- c("Cognitive\n resources")
forplot_cog$CRT <- factor(forplot_cog$CRT,levels = c("-1 SD","Mean","+1 SD"))

forplot_comb <- rbind(forplot_crt, forplot_nfc, forplot_cog)
forplot_comb$battery <- factor(forplot_comb$battery,levels = c("CRT","NfC","Cognitive\n resources"))

foodirradiation<-ggplot(forplot_comb,aes(x=fake,y=coef1, colour=Cue))+geom_line(aes(linetype=Cue, color=Cue))+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of Party Cues on Policy Support ")+geom_ribbon(aes(ymin=lb,ymax=ub, fill=Cue),alpha=.4)+geom_hline(yintercept = 0,lty="dashed")+scale_fill_manual(values=c("dark green", "red"))+scale_colour_manual(values=c("black", "black")) + theme(strip.text.y = element_text(angle = 360), legend.position="bottom")
ggsave(foodirradiation, file="Study1_Food_CRT_NFC.pdf",width=8,height=6)

save(data, file="Study1_data.RData")

